package com.shoujifeng.snowmusic.player;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.shoujifeng.snowmusic.player.http.ServerAccess;

import android.os.AsyncTask;

/**
 * 处理歌词文件的类
 */
public class LrcProcess {

	private List<LrcContent> LrcList;

	private LrcContent mLrcContent;

	public LrcProcess() {

		mLrcContent = new LrcContent();
		LrcList = new ArrayList<LrcContent>();
	}

	/**
	 * 璇诲彇姝岃瘝鏂囦欢鐨勫唴瀹?
	 */
	public String readLRC(String lrcString) {
		
		String retString = "";
		try {
			InputStream is= new ByteArrayInputStream(lrcString.getBytes("UTF-8"));
			 
			 InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
			String s = "";
			while ((s = br.readLine()) != null) {

				// 鏇挎崲瀛楃
				s = s.replace("[", "");
				s = s.replace("]", "@");

				// 鍒嗙"@"瀛楃
				String splitLrc_data[] = s.split("@");
				if (splitLrc_data.length > 1) {
					mLrcContent.setLrc(splitLrc_data[1]);

					// 澶勭悊姝岃瘝鍙栧緱姝屾洸鏃堕棿
					int LrcTime = TimeStr(splitLrc_data[0]);

					mLrcContent.setLrc_time(LrcTime);

					// 娣诲姞杩涘垪琛ㄦ暟缁?
					LrcList.add(mLrcContent);

					// 鍒涘缓瀵硅薄
					mLrcContent = new LrcContent();
				}

			}
			br.close();
			isr.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			retString = "没有找到歌词！";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			retString = "没有找到歌词！";
		}
		return retString;
	}

	
	/**
	 * 瑙ｆ瀽姝屾洸鏃堕棿澶勭悊绫?
	 */
	public int TimeStr(String timeStr) {

		timeStr = timeStr.replace(":", ".");
		timeStr = timeStr.replace(".", "@");

		String timeData[] = timeStr.split("@");

		// 鍒嗙鍑哄垎銆佺骞惰浆鎹负鏁村瀷
		int minute = Integer.parseInt(timeData[0]);
		int second = Integer.parseInt(timeData[1]);
		int millisecond = Integer.parseInt(timeData[2]);

		// 璁＄畻涓婁竴琛屼笌涓嬩竴琛岀殑鏃堕棿杞崲涓烘绉掓暟
		int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;

		return currentTime;
	}

	public List<LrcContent> getLrcContent() {

		return LrcList;
	}

	/**
	 * 鑾峰緱姝岃瘝鍜屾椂闂村苟杩斿洖鐨勭被
	 */
	public class LrcContent {
		private String Lrc;
		private int Lrc_time;

		public String getLrc() {
			return Lrc;
		}

		public void setLrc(String lrc) {
			Lrc = lrc;
		}

		public int getLrc_time() {
			return Lrc_time;
		}

		public void setLrc_time(int lrc_time) {
			Lrc_time = lrc_time;
		}
	}

}
